package com.onpositive.realmview.model.filtering;

import java.util.Set;

import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.semantic.model.api.property.PropertyProviderLookup;
import com.onpositive.semantic.model.realm.IClusterizationPoint;
import com.onpositive.semantic.model.realm.IFilter;
import com.onpositive.semantic.model.realm.ISimpleChangeListener;
import com.onpositive.semantic.model.tree.ITreeNode;

public class BooleanFilter implements IBooleanFilter{

	String description;
	String label;
	String imageId;
	String property;
	Object value;
	boolean inverse;
	private boolean defaultFilter;
	private String id;
	
	public BooleanFilter(String id,String description, String label, String imageId,
			String property, Object value, boolean inverse,boolean defaultF) {
		super();
		this.id=id;
		this.description = description;
		this.defaultFilter=defaultF;
		this.label = label;
		this.imageId = imageId;
		this.property = property;
		this.value = value;
		this.inverse = inverse;
	}

	public boolean accept(Object o) {
		if (o instanceof ITreeNode<?>){
			ITreeNode<?>c=(ITreeNode<?>) o;
			o=c.getElement();
		}
		if (o instanceof IClusterizationPoint<?>){
			return true;
		}
		IProperty property2 = PropertyProviderLookup.getProperty(o, property);
		if (property2==null){
			return true;
		}
		
		Set<Object> values = property2.getValues(o);
		boolean contains = internalAccept(values);
		return inverse?!contains:contains;
	}

	protected boolean internalAccept(Set<Object> values) {
		boolean contains = values.contains(value);
		return contains;
	}

	public String getDescription() {
		return description;
	}

	public String getImageId() {
		return imageId;
	}

	public String getLabel() {
		return label;
	}

	public void addFilterListener(
			ISimpleChangeListener<? extends IFilter> listener) {
		
	}

	public void removeFilterListener(
			ISimpleChangeListener<? extends IFilter> listener) {
		
	}

	public boolean isDefault() {
		return defaultFilter;
	}

	public String getId() {
		return id;
	}

	
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BooleanFilter other = (BooleanFilter) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

}
